package com.code.wifi.Apater;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.code.wifi.R;
import com.code.wifi.control.WifiControl;

import java.util.List;

/**
 * Created by ZJM on 2017/3/30.
 * <p>
 * Wifi 列表适配器
 */

public class WifiListAdapter extends BaseAdapter {

    private List<ScanResult> mWifiList;//扫描结果列表
    private Context context;//上下文

    /**
     * 构造器 初始化数据
     *
     * @param mWifiList wifi列表
     * @param context   上下文
     */
    public WifiListAdapter(List<ScanResult> mWifiList, Context context) {
        this.mWifiList = mWifiList;
        this.context = context;
    }

    /**
     * 设置 WiFi 列表值
     *
     * @param mWifiList 用于更新 wifi 列表的参数值
     */
    public void setMWifiList(List<ScanResult> mWifiList) {
        this.mWifiList = mWifiList;
    }

    @Override
    public int getCount() {
        if (mWifiList == null)
            return 0;
        return mWifiList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mWifiList == null)
            return null;
        return mWifiList.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (mWifiList == null)
            return 0;
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout cellLayout; //ListView 的子布局
        if (mWifiList == null) {
            return null;
        }
        ScanResult scanResult = mWifiList.get(position);//单个 wifi 信息

        if (convertView != null) {  //获取系统回收的对象
            cellLayout = (LinearLayout) convertView;
        } else {  //使用 LayoutInflater 实例化布局对象
            cellLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.subview_of_wifi, null);
        }
        //wifi 名称
        TextView wifiName = (TextView) cellLayout.findViewById(R.id.wifi_name);
        //wifi 连接状态
        TextView wifiStatus = (TextView) cellLayout.findViewById(R.id.wifi_status);
        //wifi 信号
        ImageView wifiSignal = (ImageView) cellLayout.findViewById(R.id.wifi_signal);

        if (null != scanResult.SSID) {
            Log.i(scanResult.SSID + ">>>>>>", Math.abs(scanResult.level) + "");
            wifiName.setText(scanResult.SSID);//显示 Wifi 名称
            wifiStatus.setHeight(0);//隐藏 wifi 连接状态提示栏

            //获取当前连接的 wifi
            WifiInfo wifiInfo = WifiControl.getInstance(context).getConnectionInfo();
            //判断是否 wifi 已经连接，true 则显示【已连接】
            if (wifiInfo.getSSID().equals(String.format("\"%s\"", scanResult.SSID))) {
                wifiStatus.setText(context.getResources().getString(R.string.wifi_connected));
                wifiStatus.setHeight(35);
                wifiStatus.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
            }


            //根据 wifi 强度设置 wifi 图标 绝对值越大 信号越小
            if (Math.abs(scanResult.level) > 100) {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_00));
            } else if (Math.abs(scanResult.level) > 80) {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_01));
            } else if (Math.abs(scanResult.level) > 70) {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_01));
            } else if (Math.abs(scanResult.level) > 60) {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_02));
            } else if (Math.abs(scanResult.level) > 50) {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_03));
            } else {
                wifiSignal.setImageDrawable(context.getResources().getDrawable(R.drawable.wifi_04));
            }

            return cellLayout;
        } else {
            return null;
        }
    }
}
